package cn.wjssl.test.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
public class ThreadService {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    void myThreadPoolTaskTest(CountDownLatch latch) {
        for (int i = 0; i < 5; i++) {
            threadPoolExecutor.execute(() -> {
                for (int j = 0; j < 10; j++) {
                    log.info("线程{}", Thread.currentThread().getName());
                    try {
                        Thread.sleep(1000);     // 模拟执行任务
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.info("任务执行完成");
                latch.countDown(); // 任务完成，通知主线程
            });
        }
    }
}
